package com.config;

import java.lang.reflect.Field;
import java.util.List;

/**
 * @author Xiongx
 * @version 1.0
 * @date 2021/5/1 21:28
 * @since JDK 1.8
 *
 * 字段属性过滤工具
 */
public class FieldFilterUtils {

    public static <T> void listFilter(String[] strings, Class<T> tClass, List<T> tList){
        tList.forEach(e->{
            fieldFilter(strings,tClass,e);
        });
    }

    public static <T> void fieldFilter(String[] strings,Class<T> cls,T t){
        Field[] fields=cls.getDeclaredFields();
        try {
            for (int j = 0; j < strings.length; j++) {
                String name=strings[j];
                for (int i = 0; i < fields.length; i++) {
                    Field field=fields[i];
                    String fieldName=field.getName();
                    if(name.equals(fieldName)){
                        field.setAccessible(true);
                        field.set(t,null);
                    }
                }
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}
